#!/usr/bin/env python
#
# Copyright 2009 Benjamin Davis
#
# This file is part of resistencia.
#
# resistencia is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# resistencia is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with resistencia.  If not, see <http://www.gnu.org/licenses/>.

class Item(object):
    """Item baseclass"""
    def __init__(self):
        pass
    
    def __call__(self, player, **kargs):
        """Called when player enters 'use <item>' or just '<item>'"""
        return False
    
    def describe(self):
        """Return a description of the item"""
        return "a " + self.description
    
    def name(self):
        """Return the primary name of the item"""
        return self.names[0]
    
class Items(list):
    """Adds a simple search functionality to the list class which allows
    specifying a subitem by name"""
    def __add__(self, other):
        return Items(list.__add__(self, other))
    
    def by_name(self, name):
        # For each subitem
        for item in self:
            # If one of the subitem's name matches the specified name
            if name.strip() in item.names:
                # Return the item
                return item
        return False
    
class Door(Item):
    """Door baseclass. Allows player to transition from one room to the next"""
    names = ("door", "exit")
    description = "rusting door with the remnants of paint from long ago"
    direction = "the door"
    
    def __init__(self, room):
        Item.__init__(self)
        # self.room is the room which contains the door
        self.room = room
    
    def __call__(self, player):
        player.client.send('You exit to ' + self.direction + "." )
        player.enter(self.next_room, self.direction)
        return True

class Container(Item):
    """Container baseclass. An item which can hold other items within it"""
    names = ("container",)
    description = "small container which seems to be bottomless"
    
    def __init__(self):
        Item.__init__(self)
        # To be compatible with rooms, subitems are stored in self.items
        self.items = Items()

class Coins(Item):
    """Coins baseclass. Allows specifying an amount of the particular item
    and various actions, such as concatenating two Coins items."""
    names = ("coins", "coin", "money")
    description = "pile of coins"
    
    def __init__(self, amount):
        Item.__init__(self)
        self.amount = amount
    
    def describe(self):
        if self.amount > 1:
            return self.description + " (" + str(self.amount) + ")"
        return "a " + self.names[1]
    
    def name(self):
        if self.amount > 1:
            return self.names[0] + " (" + str(self.amount) + ")"
        return self.names[1]

class AppleSack(Container):
    """Example container, contains two apples"""
    names = ("sack of apples", "sack", "bag")
    
    def __init__(self):
        Container.__init__(self)
        self.items = Items([RedApple(), GreenApple()])
    
class NorthDoor(Door):
    """Door which exits to the north"""
    names = ("door", "exit", "north", "n")
    direction = "the north"
    
    def __init__(self, room):
        Door.__init__(self, room)
    
    def __call__(self, player):
        self.next_room = self.room.get_relative_room(0, -1)
        return Door.__call__(self, player)


class SouthDoor(Door):
    """Door which exits to the south"""
    names = ("door", "exit", "south", "s")
    direction = "the south"

    def __init__(self, room):
        Door.__init__(self, room)
        
    def __call__(self, player):
        self.next_room = self.room.get_relative_room(0, 1)
        return Door.__call__(self, player)

        
class TestItem(Item):
    """Example item"""
    names = ("item", "test item")
    description = "rather shiny, but small, test item"
    
class RedApple(Item):
    
    names = ("red apple", "apple")
    description = "firm, ripe, red apple"
    
class GreenApple(Item):
    
    names = ("green apple", "apple")
    description = "tart, crunchy, green apple"
    
class Candle(Item):
    
    names = ("candle",)
    description = "waxy, white candle"

class Duplicator(Item):
    """Example item which duplicates itself when used by a player"""
    names = ("duplicator",)
    description = "shiny, round duplicator with a weird glow"
    
    def __call__(self, player, **kargs):
        player.room.items.append(Duplicator())
        for someone in player.room.players:
            someone.client.send("Something drops to the ground!")
        return True
